<template>
  <div class="col card p10">
    <label class="title">旋转货架控制</label>
    <div class="row_center mt">
      <label>列:</label>
      <el-input v-model="ctx.col" style="width: 60px;"/>
      <label class="ml">偏移:</label>
      <el-input v-model="ctx.offset" style="width: 60px;"/>
      <el-button class="ml" @click="onClickCtrl" type="primary">控制</el-button>
    </div>
    <div class="row_center mt">
      <el-select v-model="ctx.adj" style="width: 100px;">
        <el-option :label="PICK" :value="PICK"/>
        <el-option :label="SCAN" :value="SCAN"/>
        <el-option :label="AVOID" :value="AVOID"/>
        <el-option :label="RECYCLE" :value="RECYCLE"/>
      </el-select>
      <el-button type="warning" @click="onClickAdj" class="ml">校准</el-button>
      <el-button type="primary" @click="onClickAdjQuery">查询</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { dialog } from '../../service/ui';
import { rotate_adj, rotate_adj_query, rotate_ctrl } from '../../service/main_cmd';

const PICK = "取货偏移";
const SCAN = "扫码偏移";
const AVOID = "避让偏移";
const RECYCLE = "出厂设置";

interface Ctx {
  col: string,
  offset: string,
  adj: string,
}

const ctx = reactive<Ctx>({
  col: '1',
  offset: '0',
  adj: PICK,
});

async function onClickCtrl() {

  try {
    const col = parseInt(ctx.col);
    const offset = parseInt(ctx.offset);
    await rotate_ctrl(col, offset);
    dialog.succ('执行成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickAdj() {
  try {
    let type = 1;
    if (ctx.adj == SCAN) {
      type = 2;
    } else if (ctx.adj == AVOID) {
      type = 3;
    } else if (ctx.adj == RECYCLE) {
      type = 4;
    } 
    const offset = parseInt(ctx.offset);
    await rotate_adj(type, offset);
    dialog.succ('设置成功');
  } catch (msg) {
    dialog.err(msg);
  }
}


async function onClickAdjQuery() {
  try {
    const info = await rotate_adj_query();
    dialog.succ(`取货:${info.pick} 
      扫码:${info.scan} 
      避让:${info.avoid}`, 0);
  } catch (msg) {
    dialog.err(msg);
  }
}

</script>

